package com.hz.house.scheduled;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hz.house.common.ThreadPool;
import com.hz.house.service.RedisCacheDataService;
import com.hz.house.service.SellManScoreService;
import com.hz.house.service.SellManService;
import com.hz.house.service.WxgzhBuyHouseService;
import com.hz.house.service.WxgzhSellHouseService;

import lombok.extern.slf4j.Slf4j;


/**
 * 	定时任务
 * @author 10203
 *
 */
@Component
@Slf4j
public class AllTaskSchduled {

	@Autowired
	private SellManService sellManService;
	
	@Autowired
	private RedisCacheDataService redisCacheDataService;
	
	@Autowired
	private ThreadPool threadPool;
	
	@Autowired
	private SellManScoreService sellManScoreService;
	
	@Autowired
	private WxgzhBuyHouseService buyHouseWxgzh;
	
	@Autowired
	private WxgzhSellHouseService sellHouseWxgzh;
	
	
	/**0点准时执行的任务*/
	@Scheduled(cron = "1 0 0 * * ?")
	public void deleteSellManLimitCache() {
		log.info("每日0点任务开始执行!");
		this.threadPool.runInThreadPool(()->{
			log.info("每日0点:删除置业顾问积分每日限制");
			redisCacheDataService.deleteCustomerIdForRepeat();
			redisCacheDataService.deleteSellManShareLimitCache(this.sellManService.getAllSellManIds());
			redisCacheDataService.deleteCustomerId_ScoreId_SellmanIdForRepeat();
		});
		this.threadPool.runInThreadPool(()->{
			//0点更新昨日之星
			log.info("每日0点:删除昨日之星缓存");
			redisCacheDataService.deleteYesterdayStars(null);
		});
		this.threadPool.runInThreadPool(()->{
			
			log.info("每日0点:更新连续签到和连续获得积分日期");
			//更新连续获取积分的天数，把断了置为0
			try {
				sellManScoreService.updateSeriesGetScoreDays();
			} catch (Exception e) {
				log.info("更新连续获取积分天数异常，重试一次");
				try {
					sellManScoreService.updateSeriesGetScoreDays();
				} catch (Exception x) {
					x.printStackTrace();
				}
			}
			//更新置业顾问签到天数，把断了的置为0
			try {
				sellManService.updateSellManSignSeries();
			} catch (Exception e) {
				log.info("更新置业顾问连续签到天数异常，重试一次");
				try {
					sellManService.updateSellManSignSeries();
				} catch (Exception x) {
					x.printStackTrace();
				}
			}
		});
		
	}
	
	/**每分钟触发一次更新最新的访问量等数据到es*/
	@Scheduled(cron = "46 * * * * ?")
	public void updateSellManBasicInfo2Es() {
		this.threadPool.runInThreadPool(()->{
			this.sellManService.synSellManInfo2Es();
		});
	}
	
	/**每小时一次更新微信公众号token*/
	@Scheduled(cron = "5 1 * * * ?")
	public void updateWxgzhAcctk() {
		buyHouseWxgzh.updateGzhAcctk();
		sellHouseWxgzh.updateGzhAcctk();
	}
	
	/**置业顾问财富币竞价上首页的任务:23点0分1秒执行*/
	@Scheduled(cron = "1 0 23 * * ?")
	public void sellManCoinTask() {
		this.threadPool.runInThreadPool(()->{
			//删除置业顾问财富币锁
			this.sellManService.removeSellManCoinLockers();
			//删除今日最高金币缓存
			this.redisCacheDataService.deleteTodayMaxCoin();
			//有最高到最低，依次扣除金币，直到扣除一个成功者表示竞价成功
			this.sellManService.handleGoToHomeResult(0);
		});
	}
	
 
	
}
